package com.zwz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zwz.bean.AdminLoginInfo;
import com.zwz.dao.AdminLoginDao;
import com.zwz.service.AdminLoginService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class AdminLoginServiceImpl implements AdminLoginService {

    @Autowired
    private AdminLoginDao adminLoginDao;


    /**
     * 用户注册
     * @param adminLoginInfo
     * @return
     */
    @Override
    public AdminLoginInfo register(AdminLoginInfo adminLoginInfo) {
        AdminLoginInfo existUser = adminLoginDao.findUserByName(adminLoginInfo.getUsername());
        if(existUser != null){
            //如果用户名已存在
            return null;
        }else{
            adminLoginDao.regist(adminLoginInfo);
            System.out.println(adminLoginInfo.getId());
            //将登录id存入用户信息表中
            adminLoginDao.addUserInfo(adminLoginInfo.getId());

            return adminLoginInfo;
        }

    }

    @Override
    public Integer login(AdminLoginInfo adminLoginInfo) {
        Integer userId= adminLoginDao.login(adminLoginInfo);
        return userId;
    }

    @Override
    public boolean deleteById(Integer id) {
        return adminLoginDao.deleteById(id) == 1 && adminLoginDao.deleteAdminInfo(id) == 1;
    }

    @Override
    public IPage<AdminLoginInfo> getPage(int currentPage, int pageSize, AdminLoginInfo adminLoginInfo) {
        IPage<AdminLoginInfo> page = new Page<>(currentPage, pageSize);

        LambdaQueryWrapper<AdminLoginInfo> lqw = new LambdaQueryWrapper<>();

        lqw.like(Strings.isNotEmpty(adminLoginInfo.getUsername()), AdminLoginInfo::getUsername, adminLoginInfo.getUsername());
        lqw.orderByDesc(AdminLoginInfo::getId);

        adminLoginDao.selectPage(page, lqw);
        return page;
    }
}
